<?php
class PaymentAction extends CommonAction {
	
	// 支付类型列表
	public function index(){
		if($this->_get('id')){
			C('URL_MODEL',0);
			$id = $this->_get('id');
			$orderdb = M('Order');
			$this->orderinfo = $orderdb -> where(array('id' => $id)) -> find();
			if($this->orderinfo['status'] == 2){
				$this->error('该订单已经完成支付，请不要重复操作');exit;
			}
			$paymentList = M('Payment') -> where(array('status' => 1, 'site_id' => __RELATEID__)) -> select();
			if(count($paymentList) == 1){
				//更新订单支付类型
				$orderdb -> where(array('id' => $id)) -> data(array('payment_id' => $paymentList[0]['id'])) -> save();
				$this->pay($id);
			}
			
			//外部浏览器请求支付宝支付，立即跳转
			if(isset($_GET['Alipay']) && intval($_GET['Alipay']) > 0){
				$paymenetAlipay = M('Payment') -> where(array('classname' => 'Alipay', 'site_id' => __RELATEID__)) -> find();
				if($paymenetAlipay){
					$orderdb -> where(array('id' => $id)) -> data(array('payment_id' => $paymenetAlipay['id'])) -> save();
				    $this->pay($id);
					//exit;
				}else{
					$this->error('参数错误！');
				}
				
			}
			
			
			if($this->_post('pay_id')){
				$orderdb -> where(array('id' => $id)) -> data(array('payment_id' => $this->_post('pay_id'))) -> save();
				$this->pay($id);
			}
			if($this->_get('payment_id') && $this->_get('payment_id') > 0){
				//$orderdb -> where(array('id' => $id)) -> data(array('payment_id' => $this->_get('payment_id'))) -> save();
				$this->pay($id);
			}
			
			$this->paymentList = $paymentList;
			
			
			$this -> display();
		}else{
			$this->error('参数错误、订单错误或者不存在');
		}
	}
	
	//发起支付
	public function pay($orderid){
		C('TOKEN_ON', false);
		$orderdb = M('Order');
		$this->orderinfo = $orderdb -> where(array('id' => $orderid)) -> find();
		$payment = M('Payment') -> where(array('status' => 1, 'site_id' => __RELATEID__, 'id' => $this->orderinfo['payment_id'])) -> find();

		if(is_weixin() && $payment['classname'] == 'Alipay'){
			//提示跳转
			if(!isset($_GET['Alipay'])){
			    header('location:'.U('index', array('Alipay' => 1, 'id' => $_GET['id'])));
			}
			$this -> display('reindex');
			exit;
		}
		import ( '@.ORG.Payment.'.$payment['classname'] );
		$affclass = $payment['classname'];
		$aff = new $affclass(__RELATEID__);
	
		$html = $aff -> pay($orderid);
		/*if(is_weixin() && $payment['classname'] == 'Alipay'){
			//提示跳转
			$this->assign('html', $html);
			$this -> display('test');
			exit;
		}*/
		
		$this->assign('html', $html);
	}
	
	//异步回调
	public function notify(){
		$code = $_GET['code'];
		import ( '@.ORG.Payment.'.$code ); 
		$affclass = new $code();
		$mes = $affclass->callback();
	}
	
	public function display($temp = ''){
		$map  = "site_id = ".__SITEID__." and groupname = '".strtolower(GROUP_NAME)."' and (modelname = '".strtolower(MODULE_NAME)."' or modelname = '*') and (actionname = '".strtolower(ACTION_NAME)."' or actionname = '*')";
		$Systempages = M('Systempages') -> where($map) -> find();
		if($Systempages){
			$ModulesDB = D('Modules');
			$this->initView(); 
			echo $this->view -> fetch(TMPL_PATH.'Home/TemplateDefault/Public-header.html');
			echo $ModulesDB->getListToString($Systempages['page_view_id'],$this->view,false,true);
			echo $this->view -> fetch(TMPL_PATH.'Home/TemplateDefault/Public-footer.html');
		}else{
			parent::display($temp);
		}
	}
	
}